#include "StdAfx.h"
#include "AppContext.h"
#include "DllWrapper.h"

AppContext g_appContext;
//PFNCreateAppConfigObject pCreateAppConfigObject;
//PFNInitAppRegisterInfo pInitAppRegisterInfo;
//PFNCreateRegisterAppInfoObject pCreateRegisterAppInfoObject;
//PFNGetAppRegisterInfoByExePath pGetAppRegisterInfoByExePath;
//PFNGetConfigBySerialNum pGetConfigBySerialNum;

AppContext::AppContext(void)
{
	ZeroMemory(m_dbPathName, MAX_PATH);
	ZeroMemory(m_dbPath, MAX_PATH);
	ZeroMemory(m_serialNum, TAX_DEVICE_SERIAL_LENGTH);
	ZeroMemory(m_installPath, MAX_PATH);
	ZeroMemory(m_rawExePathName, MAX_PATH);
	m_isInited = FALSE;
	m_config = NULL;
	m_isMainExe = FALSE;
	//m_hMod = NULL;
//	ZeroMemory(m_appName, FAX_APP_NAME_LENGTH);
}

AppContext::~AppContext(void)
{	
	if (NULL != m_config)
	{
		//delete m_config;
	}	
	/*if (NULL != m_hMod)
	{
		FreeLibrary(m_hMod);
	}*/
}


BOOL AppContext::GetDbPathName( char *dbPathName )
{
	if (Init())
	{			
		strcpy_s(dbPathName, MAX_PATH, m_dbPathName);
		return TRUE;
	}
	return FALSE;	
}

BOOL AppContext::GetDbPath( char *dbPath )
{
	if (Init())
	{			
		strcpy_s(dbPath, MAX_PATH, m_dbPath);
		return TRUE;
	}
	return FALSE;
}

BOOL AppContext::Init()
{
	if (NULL != m_config)
	{		
		return TRUE;
	}

	if (!g_dllWrapper.Init())
	{
		//OutputDebugStringA("g_dllWrapper.Init() failed\n");
		return FALSE;
	}
	m_config = (pCreateAppConfigObject)();
	if (NULL == m_config)
	{
		return FALSE;
	}
	
	BOOL ret = TRUE;
	RegisterAppInfo *pInfo = NULL;
	do 
	{
		char exePathName[MAX_PATH]/*, installPath[MAX_PATH]*/;
		ZeroMemory(exePathName, MAX_PATH);		
		GetLongPathName(m_rawExePathName, exePathName, MAX_PATH);
		int index = strlen(exePathName) - 1;
		while(index > 0)
		{
			if ('\\' != exePathName[index])
			{
				index--;
			}
			else
			{
				break;
			}
		}
		if (0 == index)
		{
			ret = FALSE;
			break;
		}
		else
		{
			exePathName[index] = 0;
		}

		if (!(pInitAppRegisterInfo)())
		{
			//OutputDebugStringA("pInitAppRegisterInfo failed\n");
			ret = FALSE;
			break;
		}
		pInfo = (pCreateRegisterAppInfoObject)();
		if (NULL == pInfo)
		{
			//OutputDebugStringA("pCreateRegisterAppInfoObject failed\n");
			ret = FALSE;
			break;
		}

		if (m_isMainExe)
		{
			if(!(pGetAppRegisterInfoByExePath)(exePathName, *pInfo))
			{
				//OutputDebugStringA("pGetAppRegisterInfoByExePath failed --\n");
				//OutputDebugStringA(exePathName);
				ret = FALSE;
				break;
			}
		}
		else
		{
			if(!(pGetAppRegisterInfoByinstallPath)(exePathName, *pInfo))
			{
				//OutputDebugStringA("pGetAppRegisterInfoByExePath failed --\n");
				//OutputDebugStringA(exePathName);
				ret = FALSE;
				break;
			}
		}
		
		
		strcpy_s(m_serialNum, TAX_DEVICE_SERIAL_LENGTH, pInfo->m_deviceSerialNum);

		
		if (!(pGetConfigBySerialNum)(m_serialNum, *m_config))
		{
			//OutputDebugStringA("pGetConfigBySerialNum failed\n");
			ret = FALSE;
			break;
		}

		if (m_isMainExe)
		{
			if (!m_config->GetInstallPathByExePath(exePathName, m_installPath))
			{
				//OutputDebugStringA("GetInstallPathByExePath failed\n");
				ret = FALSE;
				break;
			}
		}
		else
		{
			strcpy_s(m_installPath, exePathName);
		}

		strcpy_s(m_dbPath, MAX_PATH, m_installPath);
		char tmpPath[MAX_PATH];
		m_config->GetDbFilePath(tmpPath);
		if (0 < strlen(tmpPath))
		{
			strcat_s(m_dbPath, MAX_PATH, "\\");
			strcat_s(m_dbPath, MAX_PATH, tmpPath);
		}	
		strcpy_s(m_dbPathName, MAX_PATH, m_installPath);
		strcat_s(m_dbPathName, MAX_PATH, "\\");
		m_config->GetDbFilePathName(tmpPath);
		strcat_s(m_dbPathName, MAX_PATH, tmpPath);
	} while (FALSE);
	if (NULL != pInfo)
	{
		delete pInfo;
	}
	if (ret == FALSE && NULL != m_config)
	{
		delete m_config;
		m_config = NULL;
	}
	return ret;
}

BOOL AppContext::GetSerialNum( char *serialNum )
{
	if (!Init())
	{
		//OutputDebugStringA("AppContext::GetSerialNum failed\n");
		return FALSE;
	}
	strcpy_s(serialNum, TAX_DEVICE_SERIAL_LENGTH, m_serialNum);
	return TRUE;
}

void AppContext::Backup()
{
	if (Init())
	{
		m_config->Backup(m_installPath);
	}
}

void AppContext::Recover()
{
	if (Init())
	{
		m_config->Recover(m_installPath);
	}
}

void AppContext::SetExeInfo( char *rawExePathName, BOOL isMainExe )
{
	strcpy_s(m_rawExePathName, MAX_PATH, rawExePathName);
	m_isMainExe = isMainExe;
}

//BOOL AppContext::GetInstallPath( char *installPath )
//{
//	if (!Init())
//	{
//		return FALSE;
//	}
//	strcpy_s(installPath, MAX_PATH, m_installPath);
//	return TRUE;
//}

//BOOL AppContext::GetAppName( char *appName )
//{
//	if (!Init())
//	{
//		return FALSE;
//	}
//	strcpy_s(appName, FAX_APP_NAME_LENGTH, m_appName);
//	return TRUE;
//}